package CtrLayer;
import ModelLayer.*;

public class OrderCtr
{
    private static OrderList instance = OrderList.getInstance();
    private static CustomerCtr customerCtr = new CustomerCtr();
    private static StaffCtr staffCtr = new StaffCtr();
    private static ProductCtr productCtr = new ProductCtr();

    public OrderCtr()
    {

    }
    
    public boolean deliveryDiscount(int idO, String apply)
    {
        Order ord = getOrder(idO);
        try{
            ord.setPickupDiscount(apply);
            return true;
        }
        catch(Exception e) {}
        return false;
    }
    
    public boolean quantityDiscount(int idO, double disc)
    {
        Order ord = getOrder(idO);
        try{
            ord.setQuantityDiscount(disc);
            return true;
        }
        catch(Exception e) {}
        return false;
    }

    public boolean createOrder(int id, int customer, String staff) 
    { 
        try{
            Order newOrder = new Order(id, customerCtr.getCustomer(customer), staffCtr.getStaff(staff));
            instance.addOrder(newOrder);
            return true;
        }
        catch (Exception e) { }
        return false;
    }

    public boolean addProduct(int idO, int idP, int quantity) {
        Order order = getOrder(idO);
        try{
            order.addProduct(idP, quantity);
            return true;
        }
        catch(Exception e) {}
        return false;
    }
    
    public boolean removeProduct(int idO,int idP)
    {
        Order order = getOrder(idO);
        try{
            order.removeProduct(idP);
            return true;
        }
        catch(Exception e) {}
        return false;
    }
    
    public boolean addPayment(Order order, int type, double amount, int sID) 
    {
        try{
            Payment newPayment;
            if(type==1) newPayment = new Cash(sID,amount);
            else newPayment = new Credit(sID,amount);
            
            order.addPayment(newPayment);
            order.updatePaid(amount);
            if(order.getChange()>0 && type == 2) order.setPaid(order.getFinalTotal());
            return true;
        }
        catch(Exception e) {}
        return false;
    }

    public Order getOrder(int idO) 
    {
        return instance.getOrder(idO);
    }

    public boolean changeState(int idO, String state)
    {
        Order ord = getOrder(idO);
        try{
            ord.setState(state);
            return true;
        }
        catch(Exception e) {}
        return false;
    }

}
